package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Result;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 部门管理Controller
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 获取所有部门信息
     * @return
     */
    @GetMapping
    public Result getDepts() {
        log.info("获取所有部门信息");
        return Result.success(deptService.getDepts());
    }

    /**
     * 根据部门ID删除部门
     * @param id
     */
    @Log
    @DeleteMapping("/{id}")
    public void deleteDeptById(@PathVariable Integer id) {
        log.info("根据部门ID删除部门, id={}", id);
        deptService.deleteDeptById(id);
        Result.success();
    }

    /**
     * 添加部门
     * @param dept
     */
    @Log
    @PostMapping
    public void addDept(@RequestBody Dept dept) {
        log.info("添加部门, dept={}", dept);
        deptService.addDept(dept);
        Result.success();
    }

    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable Integer id) {
        log.info("根据部门ID查询部门信息, id={}", id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public void updateDept(@RequestBody Dept dept) {
        log.info("更新部门信息, dept={}", dept);
        deptService.updateDept(dept);
        Result.success();
    }
}
